-- The MIT License
-- 
-- Copyright (c) 2008 Matthew Maravillas, Patrick Quinn
-- 
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- 
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
-- 
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.

--[[
Armies table looks like this:
{
    numArmies (int),
    focusArmy (index),
    armiesTable (table) {
        [1] {
            name (string),
            nickname (string)
            faction (index),
            color (color),
            iconColor (color),
            showScore (bool),
        }
        etc... for all armies
    }
}
--]]

local kothScore = import('/mods/koth/hook/lua/ui/game/kothscore.lua')

local OldCreateArmyLine = CreateArmyLine
local OldLayoutLine = LayoutLine
local OldSignalGameOver = SignalGameOver
local IsOverwritten = false

function CreateArmyLine(parent)
    return kothScore.AddKothToArmyLine(OldCreateArmyLine(parent))
end

function LayoutLine(line)
    OldLayoutLine(line)
    kothScore.LayoutKothLine(line)
 end

function SignalGameOver()
    OldSignalGameOver()
   
    -- Remove the beat function we add in SetupTimerBeat
    GameMain.RemoveBeatFunction(_TimerBeatFunction)

    -- Let the kothScore module know that the game is over
    kothScore.SignalGameOver()
end

function SetupTimerBeat()
    -- If this module hasn't been completely overridden by a UI mod, pass the
    -- overlay and focus army on to kothScore, otherwise, hide this score UI
    -- and have kothScore create a fresh one
    if overlay.group.IsHidden then
        kothScore.SetOverlay(overlay)
        kothScore.SetFocusArmy(focusArmy)
    else
        LOG('WARNING: KotH Mod\'s score.lua has been overwritten')
        kothScore.CreateScoreOverlay()
        IsOverwritten = true
    end
       
    -- Make sure the beat function is called at least once
    kothScore._BeatFunction() 

    -- Replace the default beat function with one capable of detecting when it's
    -- been overwritten
    GameMain.RemoveBeatFunction(_BeatFunction)
    GameMain.AddBeatFunction(kothScore._BeatFunction)
    GameMain.AddBeatFunction(_UpdateScoreBeatFunction)
    
    overlay.group.OnDestroy = function(self)
        GameMain.RemoveBeatFunction(_TimerBeatFunction)
        GameMain.RemoveBeatFunction(_UpdateScoreBeatFunction)
    end   
 end

function _UpdateScoreBeatFunction()
    kothScore.SetCurrentScores(currentScores)
    if IsOverwritten then
        Contract()
    end
end